import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { UserListDetailBean } from '../bean/transfer-check.bean';
import { NzModalService, NzNotificationService, UploadFile } from 'ng-zorro-antd';
import { HttpServcie } from '../../../service/http.service';
import { Config } from "../../../service/config";
import * as moment from 'moment';

@Component({
  selector: 'transfer-check-detail',
  templateUrl: 'transfer-check-detail.html',
  styleUrls: ['transfer-check-detail.scss']
})
export class Transferdetail implements OnInit {
  @Input() inValue: any = [];
  @Output() outValue = new EventEmitter();
  userListDetailBean: UserListDetailBean = new UserListDetailBean();//个人
  showFlag: boolean;
  //省市区
  options: any = [];
  addresOptions: any;
  // closeFlag:boolean=true;//关闭按钮
  selectValue;
  previewImage = '';
  previewVisible = false;
  config: any = Config;

  constructor(
    private confirmServ: NzModalService,
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService,
  ) { }

  ngOnInit() {
    //获取用户信息判断是超级管理员还是个人用户还是单位
    if (this.inValue) {
      let user: string = window.localStorage.getItem('user');
      if (user) {
        let userObject = JSON.parse(user);
        //企业看用户的
        if (userObject['type'] == 'party') {
          this.selectValue = 'personal';
          if (this.inValue) {
            this.userListDetailBean = Object.assign({}, this.inValue);
            // 图片回显
            if (this.inValue.faceUrl) {
              this.userListDetailBean.faceUrl = [{ url: `${this.config.IP + this.config.API.getUpload}/${this.inValue.faceUrl}`, name: `${this.inValue.faceUrl}` }];
            }
            else {
              this.userListDetailBean.faceUrl = null;
            }
            if (this.userListDetailBean.fileList && this.userListDetailBean.fileList.length > 0) {
              let files = this.userListDetailBean.fileList.split(',');
              this.userListDetailBean.fileList = [];
              files.forEach(fileurl => {
                this.userListDetailBean.fileList.push({ url: `${this.config.IP + this.config.API.getUpload}/${fileurl}`, name: `${fileurl}` });
              });
            }

            if (this.inValue.firstgt) {
              this.userListDetailBean.firstgt = moment(this.userListDetailBean.firstgt).format('YYYY-MM-DD');;
            }
            //用户类型
            this.httpServcie.roleQueryListAll({}).subscribe(data => {
              if (data.status == '200') {
                data.data.forEach(element => {
                  if (this.userListDetailBean.roleId == element.id) {
                    this.userListDetailBean.roleId = element.roleName;
                  }
                });
              }
            });
          }
        }
        if (this.inValue.state == '2' || this.inValue.state == '3') {
          this.showFlag = true;
        } else if (this.inValue.state == '1') {
          this.showFlag = false;
        }
      }
    }
    let permission = window.localStorage.getItem("funcPermissions");
  }

  loadData(e: { option: any, index: number, resolve: Function, reject: Function }): void {

    if (e.index === -1) {
      e.resolve(this.httpServcie.getProvince());
      return;
    }


    const option = e.option;
    option.loading = true;
    if (e.index === 0) {
      let values: any = option['value'];
      let valueList = this.httpServcie.getCity(e.option);
      let cities: any = {};
      cities[values] = valueList;

      setTimeout(() => {
        option.loading = false;
        e.resolve(cities[option.value]);
      }, 1);
    }
    if (e.index === 1) {
      let value = option['value'];
      let valueList = this.httpServcie.getRelation(e.option);
      let scenicspots: any = {};
      scenicspots[value] = valueList;
      setTimeout(() => {
        option.loading = false;
        e.resolve(scenicspots[option.value]);
      }, 1, scenicspots, option);
    }
  }

  //返回
  back() {
    this.outValue.emit(false);
  }

  //提交
  submit() {
    if (this.userListDetailBean.state == '3' && !this.userListDetailBean.remark) {
      this._notification.create('warning', '警告提示', "备注不能为空");
      return;
    }

    let param = {
      id: this.userListDetailBean.id,
      userId: this.userListDetailBean.userId,
      type: this.userListDetailBean.roleTypeId,
      newPartyId: this.userListDetailBean.toId,
      oldPartyId: this.userListDetailBean.fromId,
      partyTypeBfr: JSON.parse(window.localStorage["user"]).partyType,
      partyTypeAft: JSON.parse(window.localStorage["user"]).partyType,
      state: this.userListDetailBean.state,
      remark: this.userListDetailBean.remark,
      email: this.userListDetailBean.email,
      userName: this.userListDetailBean.userName,
      toName: this.userListDetailBean.toName
    };

    this.httpServcie.updateMeatasis(param).subscribe(data => {
      if (data.status == '200') {
        this.outValue.emit(false);
        return;
      }
      this._notification.create('error', '错误提示', data.message);
    })
  }

  // 预览
  handlePreview = (file: UploadFile) => {
    this.previewImage = file.url || file.thumbUrl;
    this.previewVisible = true;
  }

  // 不允许删除
  nzRemoveFile = (file: UploadFile) => {
    return false;
  }
}
